package shiyan;

import java.text.DecimalFormat;
import java.text.NumberFormat;
public class Complex {
    private double realPart;
    private double imaginPart;
    public Complex(){
        realPart = 0;
        imaginPart=0;
    }
    public Complex(double x,double y){
        this.realPart = x;
        this.imaginPart = y;
    }
    public void setReal(double r){
        this.realPart = r;
    }
    public void setImagin(double i){
        this.imaginPart = i;
    }
    public double getReal(){
        return this.realPart;
    }
    public double getImagin(){
        return this.imaginPart;
    }
    public Complex complexAdd(Complex a){
        Complex res = new Complex();
        res.realPart = this.realPart + a.realPart;
        res.imaginPart = this.imaginPart + a.imaginPart;
        return res;
    }
    public Complex complexSub(Complex a){
        Complex res = new Complex();
        res.realPart = this.realPart - a.realPart;
        res.imaginPart = this.imaginPart - a.imaginPart;
        return res;
    }
    public Complex complexMulyi(Complex a){
        Complex res = new Complex();
        res.realPart = this.realPart * a.realPart - this.imaginPart * a.imaginPart;
        res.imaginPart = this.realPart * a.imaginPart + this.imaginPart * a.realPart;
        return res;
    }
    public Complex conjugate(){
        return new Complex(this.realPart, -this.imaginPart);
    }
    public Complex complexDiv(Complex a){
        Complex res = new Complex();
        Complex aConj = a.conjugate();
        Complex num = this.complexMulyi(aConj);
        Complex den = a.complexMulyi(aConj);
        res.realPart = num.realPart/den.realPart;
        res.imaginPart = num.imaginPart/den.realPart;
        return res;
    }

    public boolean equals(Complex a){
        return this.realPart == a.realPart && this.imaginPart == a.imaginPart;
    }

    public String toString(){
        DecimalFormat ft = new DecimalFormat("#.###");
        String r = ft.format(this.realPart);  
        String i = ft.format(this.imaginPart);

        String resault = "";
        if(this.realPart!=0){ 
            if(this.imaginPart==0){  // 0
                resault = r;
            }else if(this.imaginPart==1){ // 1+i
                resault = r +"+i";
            }else if(this.imaginPart==-1){
                resault = r +"-i";
            }else if(this.imaginPart>0){
                resault = r +"+"+i+"i";
            }else{
                resault = r +""+i+"i";
            }
        }else{
            if(this.imaginPart==0){
                resault = "0";
            }else if(this.imaginPart==1){ // i + i
                resault = "i";
            }else if(this.imaginPart==-1){
                resault = "-i";
            }else{
                resault = i+"i";
            }
        }
        return resault;
    }
    public Complex addReal(double d){
    	Complex res=new Complex();
    	res.realPart=this.realPart+d;
    	res.imaginPart=this.imaginPart;
    	return res;
    }
    public double getMod(){
    	NumberFormat f=NumberFormat.getNumberInstance();
    	f.setMaximumIntegerDigits(3);
    	return Double.parseDouble(f.format(Math.sqrt(this.realPart*this.realPart+this.imaginPart*this.imaginPart)));
    }
    public double compareTO(Complex c){
    	double b=this.getMod()-c.getMod();
    	return b;
    }
    public boolean compareMax(Complex c){
    	if(this.realPart>c.realPart){
    		return true;
    	}else if(this.realPart==c.realPart){
    		if(this.imaginPart>c.imaginPart){
    			return true;
    		}else{return false;}
    	}else{
    		return false;
    	}
    }
}

